<?php
define('VERSION', 0.3);
define('REGISTERED', 2);
define('IN_PHPBB', true);
define('SERVER_IP', '78.46.108.183');

function notAccess()
{
    return
        !isset($_POST['username']) ||
        !isset($_POST['password']) ||
        !isset($_POST['email']) ||
        !isset($_POST['ip']) ||
        ($_SERVER['REMOTE_ADDR'] != SERVER_IP);
}

function isActivationRequired(array $config)
{
    return
        (
            $config['require_activation'] == USER_ACTIVATION_SELF ||
            $config['require_activation'] == USER_ACTIVATION_ADMIN
        ) &&
        $config['email_enable'];
}

function isActivateBySelf($config)
{
    return $config['require_activation'] == USER_ACTIVATION_SELF && $config['email_enable'];
}

function isActivateByAdmin($config)
{
    return $config['require_activation'] == USER_ACTIVATION_ADMIN && $config['email_enable'];
}

if(notAccess())
{
    exit('access denied');
}

$phpEx = 'php';
$phpbb_root_path = dirname(__FILE__) . DIRECTORY_SEPARATOR;

require_once($phpbb_root_path . 'common.php');
require_once($phpbb_root_path.'includes' . DIRECTORY_SEPARATOR . 'functions.' . $phpEx);
require_once($phpbb_root_path.'includes' . DIRECTORY_SEPARATOR . 'functions_user.' . $phpEx);
require_once($phpbb_root_path.'includes' . DIRECTORY_SEPARATOR . 'acm' . DIRECTORY_SEPARATOR . 'acm_file.' . $phpEx);

$email = substr($_POST['email'], 0, 100);
$username = substr($_POST['username'], 0, 255);
$password = $_POST['password'];

$user_type = USER_NORMAL;
$user_actkey = '';
$user_inactive_reason = 0;
$user_inactive_time = 0;

if(isActivationRequired($config))
{
    $user_actkey = gen_rand_string(mt_rand(6, 10));
    $user_type = USER_INACTIVE;
    $user_inactive_reason = INACTIVE_REGISTER;
    $user_inactive_time = time();
}

$user_data = array(
    'username'              => $username,
    'user_password'         => phpbb_hash($password),
    'user_email'            => $email,
    'group_id'              => REGISTERED,
    'user_timezone'         => $config['board_timezone'],
    'user_dst'              => $config['board_dst'],
    'user_lang'             => $config['default_lang'],
    'user_type'             => $user_type,
    'user_actkey'           => $user_actkey,
    'user_ip'               => substr($_POST['ip'], 0, 40),
    'user_regdate'          => time(),
    'user_inactive_reason'  => $user_inactive_reason,
    'user_inactive_time'    => $user_inactive_time
);


$user_id = user_add($user_data);

if(!$user_id)
{
    exit('error');
}

if($user_type == USER_NORMAL)
{
    exit('activates');
}

if (isActivateBySelf($config))
{
    $message = $lang['ACCOUNT_INACTIVE'];
    $email_template = 'user_welcome_inactive';
    echo 'activation_self';
}
else if (isActivateByAdmin($config))
{
    $message = $lang['ACCOUNT_INACTIVE_ADMIN'];
    $email_template = 'admin_welcome_inactive';
    echo 'activation_admin';
}

if ($config['email_enable'])
{
    include_once($phpbb_root_path . 'language' . DIRECTORY_SEPARATOR . $config['default_lang'] . DIRECTORY_SEPARATOR . 'ucp.' . $phpEx);
    include_once($phpbb_root_path . 'includes' . DIRECTORY_SEPARATOR . 'functions_messenger.' . $phpEx);

    $server_url = generate_board_url();

    $messenger = new messenger(false);
    $messenger->template($email_template, 'de');
    $messenger->to($email, $username);
    $messenger->assign_vars(array(
        'WELCOME_MSG'	=> htmlspecialchars_decode(sprintf($lang['WELCOME_SUBJECT'], $config['sitename'])),
        'USERNAME'	=> htmlspecialchars_decode($username),
        'PASSWORD'	=> htmlspecialchars_decode($password),
        'U_ACTIVATE'	=> "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
    );

    $messenger->send(NOTIFY_EMAIL);
}
?>
